/*
 * @Author: yanjikun
 * @Date: 2022-04-12 17:51:56
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-04-13 14:51:37
 * @Description: file content
 */

import { isObject } from '@/utils/is'
import { ECharts, EChartsOption } from 'echarts'
import { Ref, ref, watchEffect } from 'vue'
import { BasicChartProps } from '../basic-chart'

export type ChartState = ReturnType<typeof useChartState>
export type useChartStateParams = {
  props: BasicChartProps
}

export const useChartState = ({ props }: useChartStateParams) => {
  const basicChartRef = ref<Nullable<HTMLElement>>()
  const chartInstance = ref<Nullable<ECharts>>(null)
  const chartOptions = ref({}) as Ref<EChartsOption>
  const loadingRef = ref(false)
  const enableShow = ref(true)
  watchEffect(() => {
    if (isObject(props.dataSource)) {
      chartOptions.value = props.dataSource
    }
  })
  return {
    basicChartRef,
    chartInstance,
    loading: loadingRef,
    enableShow,
    chartOptions
  }
}
